<?php
/**
 * Created by PhpStorm.
 * User: kuafu
 * Date: 2017/2/24
 * Time: 1:15
 */

namespace app\helpers;

use yii\helpers\Url;

class Html
{
    public static function makePagination($curPage, $totalPage, $path, $queryParams=array(), $self=null)
    {

        $params = [$path];
        foreach($queryParams as $key=>$val)
        {
            if($val) $params[$key] = $val;
        }

        if($totalPage==1) return "";
        $html = "<ul class='pagination'>";
        $start = 0;
        $end = 0;

        if($curPage<=5)
        {
            $start = 1;
            if($totalPage <= 8)
            {
                $end = $totalPage;
            }
            else
            {
                $end = 8;
            }
        }
        if($curPage > 5)
        {
            if($totalPage <= 8)
            {
                $start = 1;
                $end = $totalPage;
            }

            if($totalPage > 8)
            {
                $end = $curPage + 3;
                if($end > $totalPage)
                {
                    $end = $totalPage;
                    $start = $totalPage - 7;
                }
                else
                {
                    $start = $curPage - 4;
                }
            }
        }

        if($curPage>1)
        {
            $params['page'] = $curPage-1;
            $url = Url::toRoute($params);
            $html .= "<li><a href='{$url}'>上一页</a></li>";
        }

        if($curPage>1 && $start>1)
        {
            $params['page'] = 1;
            $url = Url::toRoute($params);
            $html .= "<li><a href='{$url}'>1...</a></li>";
        }

        for($i = $start; $i <= $end; $i++)
        {
            $class = "";
            if($curPage==$i) $class="class='active'";
            $params['page'] = $i;
            $url = Url::toRoute($params);
            $html .= "<li {$class}><a href='{$url}'>{$i}</a></li>";
        }

        if($totalPage>8 && $end!=$totalPage)
        {
            $params['page'] = $totalPage;
            $url = Url::toRoute($params);
            $html .= "<li><a href='{$url}'>...{$totalPage}</a></li>";
        }


        if($curPage<$totalPage)
        {
            $params['page'] = $curPage+1;
            $url = Url::toRoute($params);
            $html .= "<li><a href='{$url}'>下一页</a></li>";
        }

        $html .="</ul>";

        return $html;
    }
}